home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / lib / conversion.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.5 KB  |  81 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import re
  20.  
  21.  
  22. def string_to_type(value):
  23.     conversion_table = (
  24.         ("(yes|true)", lambda v: True),
  25.         ("(no|false)", lambda v: False),
  26.         ("\d+", lambda v: int(v.group(0))),
  27.         ("\d+\.\d+", lambda v: float(v.group(0))),
  28.         ("(\d+) ?([kmgt]?b?)", lambda v: int(v.group(1))),
  29.         ("(\d+\.\d+) ?([kmgt]?b?)", lambda v: float(v.group(1))),
  30.         ("(\d+) ?([kmgt]?hz?)", lambda v: int(v.group(1))),
  31.         ("(\d+\.\d+) ?([kmgt]?hz?)", lambda v: float(v.group(1))))
  32.  
  33.     multiplier_table = (
  34.         ("b", 1),
  35.         ("kb?", 1024),
  36.         ("mb?", 1024 * 1024),
  37.         ("gb?", 1024 * 1024 * 1024),
  38.         ("tb?", 1024 * 1024 * 1024 * 1024),
  39.         ("hz", 1),
  40.         ("khz?", 1024),
  41.         ("mhz?", 1024 * 1024),
  42.         ("ghz?", 1024 * 1024 * 1024),
  43.         ("thz?", 1024 * 1024 * 1024 * 1024))
  44.  
  45.     if isinstance(value, basestring):
  46.         for regex, conversion in conversion_table:
  47.             match = re.match("^%s$" % regex, value, re.IGNORECASE)
  48.             if match:
  49.                 value = conversion(match)
  50.                 if len(match.groups()) < 2:
  51.                     return value
  52.  
  53.                 unit = match.group(2)
  54.                 for regex, multiplier in multiplier_table:
  55.                     match = re.match("^%s$" % regex, unit, re.IGNORECASE)
  56.                     if match:
  57.                         value *= multiplier
  58.                         return value
  59.                 else:
  60.                     raise Exception, "Unknown multiplier: %s" % unit
  61.  
  62.     return value
  63.  
  64. def sizeof_bytes(bytes):
  65.     for x in ["bytes", "KB", "MB", "GB", "TB"]:
  66.         string = "%3.1f%s" % (bytes, x)
  67.         if bytes < 1024.0:
  68.             break
  69.         bytes /= 1024.0
  70.  
  71.     return string
  72.  
  73. def sizeof_hertz(hertz):
  74.     for x in ["Hz", "KHz", "MHz", "GHz"]:
  75.         string = "%3.1f%s" % (hertz, x)
  76.         if hertz < 1000.0:
  77.             break
  78.         hertz /= 1000.0
  79.  
  80.     return string
  81.